<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>实践一下--MySQL 优化 | Celts</title><meta name="author" content="PaulGeorge"><meta name="copyright" content="PaulGeorge"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="每天一篇面试小知识  本篇着重介绍一下 MySQL优化   写在前面 每次面试一定要问的 SQL 优化到底是个啥东西？ 因为之前没有大体量数据的场景导致在面对这个问题的时候回答不出来面试官想要的答案！ 特此，好好整理一下相关知识，工作中其实也遇到过类似的问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="实践一下--MySQL 优化">
<meta property="og:url" content="https://curry3035.gitee.io/posts/56511/index.html">
<meta property="og:site_name" content="Celts">
<meta property="og:description" content="每天一篇面试小知识  本篇着重介绍一下 MySQL优化   写在前面 每次面试一定要问的 SQL 优化到底是个啥东西？ 因为之前没有大体量数据的场景导致在面对这个问题的时候回答不出来面试官想要的答案！ 特此，好好整理一下相关知识，工作中其实也遇到过类似的问题。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://curry3035.gitee.io/img/avatar.jpg">
<meta property="article:published_time" content="2022-01-04T01:00:00.000Z">
<meta property="article:modified_time" content="2023-04-08T09:45:18.120Z">
<meta property="article:author" content="PaulGeorge">
<meta property="article:tag" content="进阶">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://curry3035.gitee.io/img/avatar.jpg"><link rel="shortcut icon" href="/img/ic.ico"><link rel="canonical" href="https://curry3035.gitee.io/posts/56511/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: PaulGeorge","link":"链接: ","source":"来源: Celts","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-right"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '实践一下--MySQL 优化',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-08 17:45:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/background.css"><link rel="stylesheet" href="/css/my.css"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Celts"><span class="site-name">Celts</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">实践一下--MySQL 优化</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-04T01:00:00.000Z" title="发表于 2022-01-04 09:00:00">2022-01-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-08T09:45:18.120Z" title="更新于 2023-04-08 17:45:18">2023-04-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%9D%A2%E8%AF%95%E7%AF%87/">面试篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="实践一下--MySQL 优化"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>每天一篇面试小知识</p>
</blockquote>
<p><strong>本篇着重介绍一下 MySQL优化</strong></p>
<hr>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2022-01-05_02-25-28.png"></p>
<p><strong>写在前面</strong></p>
<p><font color=#008000>每次面试一定要问的 SQL 优化到底是个啥东西？</font></p>
<p>因为之前没有大体量数据的场景导致在面对这个问题的时候回答不出来面试官想要的答案！</p>
<p>特此，好好整理一下相关知识，工作中其实也遇到过类似的问题。</p>
<span id="more"></span>



<h3 id="SQL-执行顺序-MySQL"><a href="#SQL-执行顺序-MySQL" class="headerlink" title="SQL 执行顺序(MySQL)"></a>SQL 执行顺序(MySQL)</h3><ol>
<li><strong>FROM</strong>&lt;表名&gt; # 选取表，将多个表数据通过笛卡尔积变成一个表。</li>
<li><strong>ON</strong>&lt;筛选条件&gt; # 对笛卡尔积的虚表进行筛选</li>
<li><strong>JOIN</strong> &lt;join, left join, right join…&gt;</li>
<li>&lt;join表&gt; # 指定join，用于添加数据到on之后的虚表中，例如left join会将左表的剩余数据添加到虚表中</li>
<li><strong>WHERE</strong>&lt;where条件&gt; # 对上述虚表进行筛选</li>
<li><strong>GROUP BY</strong>&lt;分组条件&gt; # 分组</li>
<li>&lt;SUM()等聚合函数&gt; # 用于having子句进行判断，在书写上这类聚合函数是写在having判断里面的</li>
<li><strong>HAVING</strong>&lt;分组筛选&gt; # 对分组后的结果进行聚合筛选</li>
<li><strong>SELECT</strong>&lt;返回数据列表&gt; # 返回的单列必须在group by子句中，聚合函数除外</li>
<li><strong>DISTINCT</strong>数据除重</li>
<li><strong>ORDER BY</strong>&lt;排序条件&gt; # 排序</li>
<li><strong>LIMIT</strong>&lt;行数限制&gt;</li>
</ol>
<h3 id="SQL-优化策略-MySQL"><a href="#SQL-优化策略-MySQL" class="headerlink" title="SQL 优化策略(MySQL)"></a>SQL 优化策略(MySQL)</h3><p><strong>SQL 优化策略适用于数据量较大的场景下！</strong></p>
<p><strong>如果数据量较小，没必要以此为准，以免画蛇添足！</strong></p>
<p><a href="https://curry3035.gitee.io/posts/6154/"><font color=#6AAFE6>索引 ( Index )</font> </a></p>
<p><font color=#6AAFE6>1、 避免不走索引的场景</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">-- 尽量避免在字段开头模糊查询，会导致数据库引擎放弃索引进行全表扫描;</span><br><span class="line"></span><br><span class="line">SELECT * FROM user_info WHERE username LIKE &#x27;%陈%&#x27;;		[x]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">-- 优化方式：尽量在字段后面使用模糊查询;</span><br><span class="line">SELECT * FROM user_info WHERE username LIKE &#x27;陈%&#x27;; 		[√]</span><br><span class="line"></span><br><span class="line">-- 如果需求是要在前面使用模糊查询</span><br><span class="line">-- 使用MySQL内置函数INSTR(str,substr) 来匹配;</span><br><span class="line">-- INSTR()函数返回字符串中子字符串第一次出现的位置。如果在str中找不到子字符串，则INSTR()函数返回零(0);</span><br><span class="line">SELECT * FROM user_info WHERE INSTR(username,&#x27;陈&#x27;) &gt; 0;</span><br><span class="line"></span><br><span class="line">-- 当表数据量较少(少于1万),直接用like &#x27;%xx%&#x27;;</span><br></pre></td></tr></table></figure>

<br/>



<p><font color=#6AAFE6>2、尽量避免使用 in 和 not in，会导致引擎走全表扫描</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">SELECT * FROM user_info WHERE id IN (2,3);</span><br><span class="line"></span><br><span class="line">-- 优化方式：如果是连续数值，可以用between代替;</span><br><span class="line">SELECT * FROM user_info WHERE id BETWEEN 2 AND 3;</span><br><span class="line"></span><br><span class="line">-- 如果是子查询，可以用exists代替;</span><br><span class="line">-- 不走索引</span><br><span class="line">SELECT * FROM A WHERE A.id IN (SELECT id FROM B);</span><br><span class="line">-- 走索引</span><br><span class="line">SELECT * FROM A WHERE EXISTS (SELECT * FROM B WHERE B.id = A.id);</span><br></pre></td></tr></table></figure>

<br/>



<p><font color=#6AAFE6>3、 尽量避免使用 or，会导致数据库引擎放弃索引进行全表扫描</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">SELECT * FROM user_info WHERE id = 1 OR id = 3;</span><br><span class="line"></span><br><span class="line">-- 优化方式：可以用union代替or</span><br><span class="line">-- UNION语句：用于将不同表中相同列中的数据展示出来;(不包括重复数据)</span><br><span class="line">-- UNION ALL语句：用于将不同表中相同列中的数据展示出来;(包括重复数据)</span><br><span class="line">SELECT * FROM user_info WHERE id = 1;</span><br><span class="line">   UNION</span><br><span class="line">SELECT * FROM user_info WHERE id = 3;</span><br></pre></td></tr></table></figure>

<br/>



<p><font color=#6AAFE6>4、尽量避免进行 null 值的判断，会导致数据库引擎放弃索引进行全表扫描</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">SELECT * FROM user_info WHERE score IS NULL;</span><br><span class="line"></span><br><span class="line">-- 优化方式：可以给字段添加默认值0，对0值进行判断;</span><br><span class="line">SELECT * FROM user_info WHERE score = 0;</span><br></pre></td></tr></table></figure>



<p><font color=#6AAFE6>5、尽量避免在where条件中等号的左侧进行表达式、函数操作，会导致数据库引擎放弃索引进行全表扫描</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">-- 可以将表达式、函数操作移动到等号右侧;</span><br><span class="line"></span><br><span class="line">-- 全表扫描</span><br><span class="line">SELECT * FROM user_info WHERE score/10 = 9;</span><br><span class="line">-- 走索引</span><br><span class="line">SELECT * FROM user_info WHERE score = 10*9;</span><br></pre></td></tr></table></figure>



<p><font color=#6AAFE6>6、当数据量大时，避免使用 where 1=1 的条件。通常为了方便拼装查询条件，我们会默认使用该条件，数据库引擎会放弃索引进行全表扫描</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">SELECT username, age, sex FROM user_info WHERE 1=1</span><br><span class="line"></span><br><span class="line">-- 优化方式：用代码拼装sql时进行判断，没 where 条件就去掉 where，有where条件就加 and</span><br><span class="line">-- 比如这里比较推荐使用 MyBatis;</span><br></pre></td></tr></table></figure>



<p><font color=#6AAFE6>7、查询条件不能用 &lt;&gt; 或者 !=</font></p>
<p><font color=#6AAFE6>8、where条件仅包含复合索引非前置列</font></p>
<p><font color=#6AAFE6>9、order by 条件要与where中条件一致，否则order by不会利用索引进行排序</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">-- 不走age索引;</span><br><span class="line">SELECT * FROM user_info order by age;</span><br><span class="line"> </span><br><span class="line">-- 走age索引;</span><br><span class="line">SELECT * FROM user_info where age &gt; 0 order by age;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="SELECT-语句优化-MySQL"><a href="#SELECT-语句优化-MySQL" class="headerlink" title="SELECT 语句优化(MySQL)"></a>SELECT 语句优化(MySQL)</h3><p><font color=#6AAFE6>1、避免出现select </font></p>
<p><font color=#6AAFE6>2、多表关联查询时，小表在前，大表在后</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 1.当使用 left join 时，左表是驱动表，右表是被驱动表;</span><br><span class="line"> * 2.当使用 right join 时，右表时驱动表，左表是驱动表;</span><br><span class="line"> * 3.当使用 inner join 时，mysql 会选择数据量比较小的表作为驱动表，大表作为被驱动表;</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">-- 在执行效率上，小表驱动大表 优于 大表驱动小表;</span><br><span class="line">-- 驱动表索引没有生效，被驱动表索引有效;</span><br></pre></td></tr></table></figure>

<br/>



<p><font color=#6AAFE6>3、使用表的别名</font></p>
<p><font color=#6AAFE6>4、用where字句替换HAVING字句</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 因为HAVING只会在检索出所有记录之后才对结果集进行过滤，而where则是在聚合前刷选记录;</span><br><span class="line"> * 如果能通过where字句限制记录的数目，那就能减少这方面的开销;</span><br><span class="line"> */</span><br></pre></td></tr></table></figure>

<p><font color=#6AAFE6>5、调整Where字句中的连接顺序</font></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">/**</span><br><span class="line"> * MySQL采用从左往右，自上而下的顺序解析where子句。根据这个原理，应将过滤数据多的条件往前放，最快速度缩小结果集;</span><br><span class="line"> */</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="增删改-DML-语句优化"><a href="#增删改-DML-语句优化" class="headerlink" title="增删改 DML 语句优化"></a>增删改 DML 语句优化</h3><p><font color=#6AAFE6>1、大批量插入数据</font></p>
<p>如果同时执行大量的插入，建议使用多个值的INSERT语句(方法二)</p>
<p>方法一：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">INSERT INTO user_info VALUES(1,2); </span><br><span class="line"> </span><br><span class="line">INSERT INTO user_info VALUES(1,3); </span><br><span class="line"> </span><br><span class="line">INSERT INTO user_info VALUES(1,4);</span><br></pre></td></tr></table></figure>

<p>方法二：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">INSERT INTO user_info VALUES(1,2),(1,3),(1,4);</span><br></pre></td></tr></table></figure>



<p><font color=#6AAFE6>2、避免重复查询更新的数据</font></p>
<p>针对业务中经常出现的更新行同时又希望获得改行信息的需求，MySQL并不支持PostgreSQL那样的UPDATE RETURNING语法，在MySQL中可以通过变量实现。</p>
<p>例如，更新一行记录的时间戳，同时希望查询当前记录中存放的时间戳是什么，简单方法实现：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Update t1 set time=now() where col1=1; </span><br><span class="line"> </span><br><span class="line">Select time from t1 where id =1; </span><br></pre></td></tr></table></figure>

<p>使用变量，可以重写为以下方式：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Update t1 set time=now () where col1=1 and @now: = now (); </span><br><span class="line"> </span><br><span class="line">Select @now;</span><br></pre></td></tr></table></figure>

<p>前后二者都需要两次网络来回，但使用变量避免了再次访问数据表，特别是当t1表数据量较大时，后者比前者快很多。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io">PaulGeorge</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io/posts/56511/">https://curry3035.gitee.io/posts/56511/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://curry3035.gitee.io" target="_blank">Celts</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E8%BF%9B%E9%98%B6/">进阶</a><a class="post-meta__tags" href="/tags/MySQL/">MySQL</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/59617/" title="实践一下--Docker基础版"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">实践一下--Docker基础版</div></div></a></div><div class="next-post pull-right"><a href="/posts/30553/" title="hexo 博客总结"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">hexo 博客总结</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/41767/" title="每日一面--MySQL 索引"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-05</div><div class="title">每日一面--MySQL 索引</div></div></a></div><div><a href="/posts/55119/" title="每日一面--Files工具类"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-01</div><div class="title">每日一面--Files工具类</div></div></a></div><div><a href="/posts/34600/" title="面试一下--JUC入门"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-10</div><div class="title">面试一下--JUC入门</div></div></a></div><div><a href="/posts/11315/" title="实践一下--Spring Security"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-01</div><div class="title">实践一下--Spring Security</div></div></a></div><div><a href="/posts/40042/" title="每日一面--反射"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-01</div><div class="title">每日一面--反射</div></div></a></div><div><a href="/posts/27951/" title="学习--ElasticSearch"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">学习--ElasticSearch</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">PaulGeorge</div><div class="author-info__description">很多事情就像是旅行一样，当你决定要出发的时候，最困难的那部分其实就已经完成了</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/PaulGeorge123"><i class="fab fa-github"></i><span>GitHub URL</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/PaulGeorge123" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F-MySQL"><span class="toc-number">1.</span> <span class="toc-text">SQL 执行顺序(MySQL)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-%E4%BC%98%E5%8C%96%E7%AD%96%E7%95%A5-MySQL"><span class="toc-number">2.</span> <span class="toc-text">SQL 优化策略(MySQL)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SELECT-%E8%AF%AD%E5%8F%A5%E4%BC%98%E5%8C%96-MySQL"><span class="toc-number">3.</span> <span class="toc-text">SELECT 语句优化(MySQL)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A2%9E%E5%88%A0%E6%94%B9-DML-%E8%AF%AD%E5%8F%A5%E4%BC%98%E5%8C%96"><span class="toc-number">4.</span> <span class="toc-text">增删改 DML 语句优化</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/47231/" title="POI读取Excel问题">POI读取Excel问题</a><time datetime="2023-04-11T01:00:00.000Z" title="发表于 2023-04-11 09:00:00">2023-04-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/8422/" title="Excel大文件的上传">Excel大文件的上传</a><time datetime="2023-04-10T01:00:00.000Z" title="发表于 2023-04-10 09:00:00">2023-04-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/55119/" title="每日一面--Files工具类">每日一面--Files工具类</a><time datetime="2023-01-01T01:00:00.000Z" title="发表于 2023-01-01 09:00:00">2023-01-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/34600/" title="面试一下--JUC入门">面试一下--JUC入门</a><time datetime="2022-09-10T01:00:00.000Z" title="发表于 2022-09-10 09:00:00">2022-09-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/16284/" title="每日一面--字符流和字节流">每日一面--字符流和字节流</a><time datetime="2022-07-01T01:00:00.000Z" title="发表于 2022-07-01 09:00:00">2022-07-01</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By PaulGeorge</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>